package Engine.GUI;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import Core.Core;
import Engine.Drawable;
import Engine.Updateable;
import Engine.InputHandling.Mouse;
import Engine.Utils.ImageHandler;
import Engine.Utils.Vector2f;

/**
 * 
 * @author Lueko
 *
 */
public class Button implements Drawable, Updateable {

	private BufferedImage image;
	private Vector2f pos;
	private boolean removed = false;
	private boolean clicked = false;
	
	public Button(Vector2f pos) {
		this.pos = pos;
	}
	
	public Button(BufferedImage image, Vector2f pos) {
		this.pos = pos;
		this.image = image;
	}
	
	public Button(String imagePath, Vector2f pos) {
		this.pos = pos;
		image = ImageHandler.load(imagePath);
	}
	
	@Override
	public void tick() {
		Vector2f mousePos = Mouse.getPos();
		if (mousePos.getX() >= pos.getX()  && mousePos.getX() <= pos.getX() + image.getWidth() && 
				mousePos.getY() >= pos.getY() && mousePos.getY() <= pos.getY() + image.getHeight()) {
			if (Mouse.mousePressed(1) || Mouse.mouseClicked(1) || Mouse.mouseDragged(1)) {
				clicked = true;
				System.out.println("Button clicked!");
			} else {
				clicked = false;
			}
		} else {
			clicked = false;
		}
	}

	@Override
	public void render(Graphics g) {
		if (!clicked) g.drawImage(image, (int)pos.getX(), (int)pos.getY(), null);
	}
	
	public BufferedImage getImage() {
		return image;
	}
	
	public void setImage(BufferedImage image) {
		this.image = image;
	}
	
	public void add() {
        Core.getDrawables().add(this);
        Core.getUpdateables().add(this);
    }

	public void remove() {
		removed = true;
	}

	@Override
	public boolean removed() {
		return removed;
	}

	@Override
	public Vector2f getPos() {
		return pos;
	}
	
}
